<?php


namespace app\adminapi\controller\room;


use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\room\department\RoomDepartmentDeviceLists;
use app\adminapi\logic\room\department\RoomDepartmentDeviceLogic;
use app\adminapi\validate\room\department\RoomDepartmentDeviceValidate;
use app\common\enum\DefaultEnum;
use app\common\model\Campus;
use app\common\model\room\department\RoomDepartmentDevice;
use app\common\service\ConfigService;

/**
 * 科室设备关联控制器
 * Class RoomDepartmentDeviceController
 * @package app\adminapi\controller\room
 */
class RoomDepartmentDeviceController extends BaseAdminController {


    /**
     * @notes 获取科室设备关联列表
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public function lists() {
        return $this->dataLists(new RoomDepartmentDeviceLists());
    }


    /**
     * @notes 添加科室设备关联
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public function add() {
        $params = (new RoomDepartmentDeviceValidate())->post()->goCheck('add');
        $result = RoomDepartmentDeviceLogic::add($params);
        if (true === $result) {
            return $this->success('添加成功', [], 1, 1);
        }
        return $this->fail(RoomDepartmentDeviceLogic::getError());
    }


    /**
     * @notes 编辑科室设备关联
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public function edit() {
        $params = (new RoomDepartmentDeviceValidate())->post()->goCheck('edit');
        $result = RoomDepartmentDeviceLogic::edit($params);
        if (true === $result) {
            return $this->success('编辑成功', [], 1, 1);
        }
        return $this->fail(RoomDepartmentDeviceLogic::getError());
    }

    public function editField() {
        $params = input();
        $result = RoomDepartmentDeviceLogic::editField($params);
        if (true === $result) {
            return $this->success('编辑成功', [], 1, 1);
        }
        return $this->fail(RoomDepartmentDeviceLogic::getError());
    }

    public function setSave() {
        $params = $this->request->param();
        $res = RoomDepartmentDeviceLogic::setSave($params);
        if (!$res) {
            return $this->fail(RoomDepartmentDeviceLogic::getError());
        }
        return $this->success('编辑成功', [], 1, 1);
    }

    /**
     * @notes 删除科室设备关联
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public function delete() {
        $params = (new RoomDepartmentDeviceValidate())->post()->goCheck('delete');
        RoomDepartmentDeviceLogic::delete($params);
        return $this->success('删除成功', [], 1, 1);
    }


    /**
     * @notes 获取科室设备关联详情
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public function detail() {
        $params = (new RoomDepartmentDeviceValidate())->goCheck('detail');
        $result = RoomDepartmentDeviceLogic::detail($params);
        return $this->data($result);
    }

    /**
     * 更改状态
     *
     * @return void
     */
    public function updateStatus() {
        $params = input();
        $result = RoomDepartmentDeviceLogic::updateStatus($params);
        if (true === $result) {
            return $this->success('修改成功', [], 1, 1);
        }
        return $this->fail(RoomDepartmentDeviceLogic::getError());
    }


    public function updateInit() {
        set_time_limit(30000);
        ini_set("max_execution_time", 30000);

        $params = input();
        $result = RoomDepartmentDeviceLogic::updateInit($params);
        if (true === $result) {
            return $this->success('修改成功', [], 1, 1);
        }
        return $this->fail(RoomDepartmentDeviceLogic::getError());
    }

    public function copyAdd() {
        $params = input();
        $time = date("Y-m-d H:i:s");

        if ($params['device_type'] == 1) {
            $dc = ConfigService::get('room_apartment', 's_device_coefficient');
            $dp = ConfigService::get('room_apartment', 's_device_price');
        } else {
            $dc = ConfigService::get('room_apartment', 'd_device_coefficient');
            $dp = ConfigService::get('room_apartment', 'd_device_price');
            $params['device_type'] = $params['device_type'] ? $params['device_type'] : 2;
        }
        $room_model = new RoomDepartmentDevice();
        $lists = $room_model->field([
            "cid", "bid", "rid", "sort",
            "'" . $params['device_type'] . "' as device_type",
            "'" . $dc . "' as coefficient",
            "'" . $dp . "' as price",
            "'0' as room_master", "'" . $time . "' as create_time", "'" . $time . "' as update_time",
        ])->whereIn("id", $params['id'])->append([
            "copy_add"
        ])->select()->toArray();
        if (!$lists) {
            return $this->fail('查询失败');
        }
        $res = $room_model->strict(false)->insertAll($lists);
        if (!$res) {
            return $this->fail('操作失败');
        }
        return $this->success('新增成功');
    }

    /**
     * 全部设备
     *
     * @return void
     */
    public function deviceAll() {
        $result = [];
        $campus = Campus::where('status', DefaultEnum::SHOW)->with('device')->order('id', 'ASC')->select()->toArray();
        foreach ($campus as $item) {
            $children = [];
            if (!empty($item['device'])) {
                foreach ($item['device'] as $device) {
                    $children[] = [
                        'value' => $device['id'],
                        'label' => $device['name'],
                        'children' => []
                    ];
                }
            }
            $result[] = [
                'value' => $item['id'],
                'label' => $item['name'],
                'children' => $children
            ];
        }
        return $this->data($result);
    }

    /**
     * 全部科室房间
     *
     * @return void
     */
    public function departmentAll() {
        $cid = $this->adminCid;

        $c_where = [];
        $c_where[] = ["status", "=", 1];
        if ($cid !== 0) {
            $c_where[] = ["id", "=", $cid];
        }

        $result = [];
        $campus = Campus::where('status', DefaultEnum::SHOW)->where($c_where)
            ->with('buildingDepartment')->order('id', 'ASC')
            ->select()->toArray();
        foreach ($campus as $item) {
            $children = [];
            if (!empty($item['buildingDepartment'])) {
                foreach ($item['buildingDepartment'] as $building) {
                    $children_item = [];
                    if (!empty($building['department'])) {
                        foreach ($building['department'] as $device) {
                            $children_item[] = [
                                'value' => $device['id'],
                                'label' => $device['name'],
                                'children' => []
                            ];
                        }
                    }
                    $children[] = [
                        'value' => $building['id'],
                        'label' => $building['name'],
                        'children' => $children_item,
                        'disabled' => empty($children_item) ? true : false
                    ];
                }
            }
            $result[] = [
                'value' => $item['id'],
                'label' => $item['name'],
                'children' => $children
            ];
        }
        return $this->data($result);
    }


    public function department() {
        $cid = $this->adminCid;

        $c_where = [];
        $c_where[] = ["status", "=", 1];
        if ($cid !== 0) {
            $c_where[] = ["id", "=", $cid];
        }

        $result = [];
        $campus = Campus::where('status', DefaultEnum::SHOW)->where($c_where)
            ->with('department')->order('id', 'ASC')
            ->select()->toArray();
        foreach ($campus as $item) {
            $children_item = [];
            if (!empty($item['department'])) {
                foreach ($item['department'] as $department) {
                    $children_item[] = [
                        'value' => $department['id'],
                        'label' => $department['name'],
                        'children' => []
                    ];
                }
            }
            $result[] = [
                'value' => $item['id'],
                'label' => $item['name'],
                'children' => $children_item
            ];
        }
        return $this->data($result);
    }


}
